﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

using EzDesk.Generic;
using EzDesk.DesktopClient;
using EzDesk.Generic.WPF;
using System.Threading;
using EzDesk.DesktopClient.ViewContract;
using EzDesk.Generic.WPF;
namespace EzDesk.DesktopClient.View
{
    /// <summary>
    /// Interaction logic for NotificationView.xaml
    /// </summary>
    public partial class NotificationView : WindowExtenBase,INotificationView
    {
        private TimeSpan _waitTime = TimeSpan.FromSeconds(8);
        private DispatcherTimer _disappearTimer = new DispatcherTimer();

        public NotificationView()
        {
            InitializeComponent();
            this.Init();
        }
        private void Init()
        {
            InitWindowState();
            InitEvent();
            InitState();
        }
        private void InitWindowState()
        {
            this.ShowInTaskbar = false;
        }
        private void InitEvent()
        {
            InitDispatcherTimer();
        }
        private void InitDispatcherTimer()
        {
            _disappearTimer.Interval = _waitTime;
            _disappearTimer.Tick += (obj, e) =>
                {
                    this.IsMouseOver.TrueOrFalseDo_Action(this.NotificationShow, this.NotificationHide);
                };
        }

        /// <summary>
        /// @Refactory need to move outside
        /// </summary>
        private void InitState()
        {
            this.MoveToRightBottom();
            this.Opacity = 0;
        }
        private void NextBtnClick(object sender, RoutedEventArgs e)
        {
            this.OnGoNext.ExecuteMethod(sender, e);
        }
        private void PreviewBtnClick(object sender, RoutedEventArgs e)
        {
            this.OnGoPrev.ExecuteMethod(sender, e);
        }

        private void CloseBtn_Click(object sender, RoutedEventArgs e)
        {
            this.MinimizeCurrentWindow();
            this.NotificationHide();
        }

        #region INotificationView Members
        public event EventHandler OnGoNext;
        public event EventHandler OnGoPrev;

        public object ViewModel
        {
            get
            {
                return this.DataContext;
            }
            set
            {
                this.DataContext = value;
            }
        }

        public void NotificationShow()
        {
            this.Topmost = true;
            this.Show();
            this.ShowSlowly();
            this.PreHide();
        }

        private void PreHide()
        {
            ThreadPool.QueueUserWorkItem((state) => {
                this._disappearTimer.Stop();
                System.Threading.Thread.Sleep(_waitTime);
                this._disappearTimer.Start();
            });
        }

        public void NotificationHide()
        {
            this.Topmost = false;
            this.HideSlowly();
        }
        #endregion INotificationView Members
    }
}
